package net.lysa.ooo.controller;

import java.util.List;

import javax.inject.Inject;

import net.lysa.ooo.business.UserBusiness;
import net.lysa.ooo.service.SayHi;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import domain.User;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	@Inject// @Named("sayHi2")
	SayHi sayHi;
	
	@Inject
	UserBusiness userBusiness;
	
	@RequestMapping(value = "/")
	public String home() {
		logger.info("HomeController: Passing through...{}-{}-{}-{}",new Object[]{"1","2","3","4"});
		logger.info("{}",sayHi.hi());
		//return "WEB-INF/views/home.jsp"; //无ViewConfig
		return "home";
	}
	
	@RequestMapping(value="/hi",method=RequestMethod.GET)
	public String hi(
			@RequestParam("name") String name,
			@RequestParam("time") String time,
			Model model
			)
	{
		model.addAttribute("name", name);
		model.addAttribute("time", time);
		return "hi";
	}
	
	@RequestMapping(value="/test",method=RequestMethod.GET)
	public String test(@RequestParam("email") String email){
		User user = new User(email);
		userBusiness.signUp(user);
		//List<User> list = userBusiness.showUsers();
		//model.addAttribute("email", email+":"+user.getId());
		//return "success";
		return "redirect:/showUsers";
	}
	
	@RequestMapping(value="/userInfo/{id}",method=RequestMethod.GET,produces="application/xml")
	@ResponseBody public User UserInfo(@PathVariable String id){
		return userBusiness.getUser(id);
	}
	
	@RequestMapping(value="/showUsers",method=RequestMethod.GET)
	public String showUser(Model model){
		List<User> list = userBusiness.showUsers();
		model.addAttribute("users", list);
		return "user_list";
	}
	
	@RequestMapping(value="/signIn",method=RequestMethod.GET)
	public String signIn(Model model){
		return "signin";
	}
	
	@RequestMapping(value="removeUser",method=RequestMethod.GET)
	public String removeUser(@RequestParam String id){
		userBusiness.removeUser(id);
		return "redirect:/showUsers";
	}
	
	@RequestMapping(value="/showAll",method=RequestMethod.GET)
	public List<User> showUser(){
		List<User> list = userBusiness.showUsers();
		return list;
	}
	
}
